
PROJECT    := OWNet
MAIN       := OWNet

LIBS       := Config Sensor Sensors DB CfgPanel ViewPanel
LIBSTD     := ul.math.crc ul.str ul.iobj ul.gettext ul.comm.serial ul.comm.serial.rxtx ul.comm.1wire ul.graph.chart

CLASSPATH_LIN := .:./lib/RXTXcomm.jar:./lib/sqlitejdbc.jar:./lib/bislider.jar:./lib/xmlgraphics-commons.jar:./lib/jchart2d.jar
CLASSPATH_WIN := .;./lib/RXTXcomm.jar;./lib/sqlitejdbc.jar;./lib/bislider.jar;./lib/xmlgraphics-commons.jar;./lib/jchart2d.jar

DIR_SRC    := ./src
DIR_BUILD  := ./build
DIR_DIST   := ./dist
DIR_LOCALE := locale
DIR_JAR    := ./jar

DIR_LIBS   := $(DIR_SRC)/libs
DIR_LIBSTD := $(DIR_SRC)/libstd
DIR_LIBSEXT := ./libext

SCALAC     := scalac
SCALA      := scala
JAR        := jar
MV         := mv
ifndef ComSpec
	CLASSPATH := $(CLASSPATH_LIN)
	RM := rm -rf
	CP := cp -r -L
	MD := mkdir -p
	CD := cd
	ECHO := echo -e
	DIR_SEP=:
else
	CLASSPATH := $(CLASSPATH_WIN)
	RM := del /q /f
	CP := cp -r -L
	MD := md
	CD := cd
	ECHO := echo
	DIR_SEP=;
endif

BUILD_OPTS := -encoding UTF-8 -unchecked -deprecation -Xmigration

build: main

allrun: all run

all: clean libsext libstd libs main locale resources jars distsrc distjars

dirtree:
	$(MD) ./buid ./dist ./jar
	
main:
	@$(CD) $(DIR_BUILD); $(SCALAC) $(BUILD_OPTS) -d . -classpath $(CLASSPATH) ../$(DIR_SRC)/$(MAIN).scala
	@$(ECHO) "\n\n----== Main compilation end ==----\n\n\a\a"

config:
	@$(CP) -f $(DIR_SRC)/$(PROJECT).conf $(DIR_BUILD)/$(PROJECT).conf
	@$(CP) -f $(DIR_SRC)/$(PROJECT).conf $(DIR_JAR)/$(PROJECT).conf
	@$(CP) -f $(DIR_SRC)/$(PROJECT).csv $(DIR_BUILD)/$(PROJECT).csv
	@$(CP) -f $(DIR_SRC)/$(PROJECT).csv $(DIR_JAR)/$(PROJECT).csv

locale:
	@xgettext -o $(DIR_SRC)/$(DIR_LOCALE)/$(MAIN).pot -L Java --from-code=utf-8 --keyword=tr $(DIR_SRC)/$(MAIN).scala $(LIBS:%=$(DIR_LIBS)/%.scala)
	@$(CP) $(DIR_SRC)/$(DIR_LOCALE) $(DIR_BUILD)

libsext:
	@$(MD) $(DIR_BUILD)/lib
	@$(CP) $(DIR_LIBSEXT)/*.jar $(DIR_BUILD)/lib

libstd:
	@$(CD) $(DIR_BUILD); $(SCALAC) $(BUILD_OPTS) -d . -classpath $(CLASSPATH) $(LIBSTD:%=../$(DIR_LIBSTD)/%.scala)
	@$(ECHO) "\n\n----== Standard libraries compilation end ==----\n\n\a\a"

libs:
	@$(CD) $(DIR_BUILD); $(SCALAC) $(BUILD_OPTS) -d . -classpath $(CLASSPATH) $(LIBS:%=../$(DIR_LIBS)/%.scala)
	@$(ECHO) "\n\n----== Libraries compilation end ==----\n\n\a\a"

libsrun: libs main run

jars:
	@$(CD) $(DIR_BUILD); $(JAR) cvf ../$(DIR_JAR)/$(PROJECT).jar `find . -name "*.class" -type f`
	@$(CP) $(DIR_BUILD)/lib $(DIR_JAR)
	@$(CP) $(DIR_BUILD)/locale $(DIR_JAR)
	@$(CP) $(DIR_BUILD)/*.conf $(DIR_JAR)
	@$(CP) $(DIR_BUILD)/*.csv $(DIR_JAR)
	@$(CP) README $(DIR_JAR)
	@$(CP) INSTALL $(DIR_JAR)
	@$(CP) LICENSE $(DIR_JAR)
	@$(CP) $(SCALA_HOME)/lib/scala-library.jar $(DIR_JAR)/lib
	@$(CP) $(SCALA_HOME)/lib/scala-swing.jar $(DIR_JAR)/lib
	@$(ECHO) "@java -classpath $(CLASSPATH_WIN);./lib/scala-library.jar;./lib/scala-swing.jar;$(PROJECT).jar $(PROJECT)" >$(DIR_JAR)/$(PROJECT).bat
	@$(ECHO) '#!/usr/bin/env sh\njava -classpath $(CLASSPATH_LIN):./lib/scala-library.jar:./lib/scala-swing.jar:$(PROJECT).jar  $(PROJECT)' >$(DIR_JAR)/$(PROJECT).sh
	@chmod 751 $(DIR_JAR)/$(PROJECT).sh

distsrc:
	@$(MD) $(DIR_DIST)/$(PROJECT)
	@$(CP) $(DIR_SRC) $(DIR_DIST)/$(PROJECT)
	@$(CP) Makefile $(DIR_DIST)/$(PROJECT)
	@$(CP) README $(DIR_DIST)/$(PROJECT)
	@$(CP) INSTALL $(DIR_DIST)/$(PROJECT)
	@$(CP) LICENSE $(DIR_DIST)/$(PROJECT)
	@$(RM) $(DIR_DIST)/$(PROJECT)_src.zip
	@$(CD) $(DIR_DIST); zip -r $(PROJECT)_src.zip $(PROJECT)
	@$(RM) $(DIR_DIST)/$(PROJECT)
	
distjars:
	@$(RM) $(DIR_DIST)/$(PROJECT)_jar.zip
	@$(MD) $(DIR_DIST)/$(PROJECT)
	@$(CP) $(DIR_JAR)/* $(DIR_DIST)/$(PROJECT)
	@$(MD) $(DIR_DIST)/$(PROJECT)/install
	@$(CP) ./install/*.dll $(DIR_DIST)/$(PROJECT)/install
	@$(CP) ./install/*.so $(DIR_DIST)/$(PROJECT)/install
	@$(CD) $(DIR_DIST); zip -r $(PROJECT)_jar.zip $(PROJECT)
	@$(RM) $(DIR_DIST)/$(PROJECT)

resources:
	@$(CP) -f $(DIR_SRC)/$(PROJECT).conf $(DIR_BUILD)/
	@$(CP) -f $(DIR_SRC)/$(PROJECT).csv $(DIR_BUILD)/
	
android:

clean:
	@$(RM) $(DIR_BUILD)/*
	@$(RM) $(DIR_JAR)/*
	@$(RM) $(DIR_DIST)/*
	
run:
	@$(CP) $(DIR_SRC)/$(PROJECT).conf $(DIR_BUILD)/
	@$(CP) $(DIR_SRC)/$(PROJECT).csv $(DIR_BUILD)/
	@$(CD) $(DIR_BUILD); scala -classpath $(CLASSPATH) $(MAIN)
	@$(CP) $(DIR_BUILD)/$(PROJECT).conf $(DIR_SRC)/$(PROJECT).conf
	@$(CP) $(DIR_BUILD)/$(PROJECT).csv $(DIR_SRC)/$(PROJECT).csv

runjar:
	@$(CD) $(DIR_JAR); scala -classpath $(CLASSPATH)$(DIR_SEP)$(PROJECT).jar $(MAIN)



commit:
	$(ECHO) "Commiting"
	$(MD) $(DIR_SRC)/tmp
	$(MV) $(DIR_LIBSTD)/* $(DIR_SRC)/tmp
	$(CP) $(DIR_SRC)/tmp/* $(DIR_LIBSTD)
	hg commit -m "$(MSG)"
	$(RM) $(DIR_LIBSTD)/*
	$(MV) $(DIR_SRC)/tmp/* $(DIR_LIBSTD)
	$(RM) $(DIR_SRC)/tmp
	
push:
	./push.sh
	
